<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of usuario
 *
 * @author Marlon
 */
class usuario extends CI_Controller{
    
    public function Iniciar(){        
        $login=$this->loggedIn();        
        $args['username'] = $login['username'];       
        $args['confirm'] = 'creacionUsuario';
        $args['mensaje'] ='Se registró el usuario';
        $args['title'] = 'home';
        $args['content'] = 'home/home';
        $this->loadView('view',$args);
    }
     function __construct() {
        parent::__construct(CI_Controller::$LOGIN);
        $this->load->model('Usuario_model');
        $this->load->library('encrypt');
    }
    function encriptar($cadena)
    {
         return $this->encrypt->encode($cadena);
    }

    function desencriptar($cadena)

    {

      return  $this->encrypt->decode($cadena);

    }
    function registrarUsuario(){
        $tipPer= $this->input->post('tipPer');
        $ruc = $this->input->post('ruc');
        $direc = $this->input->post('direc');
        $telef = $this->input->post('telef');
        $razon = $this->input->post('razon');
        $gener = $this->input->post('gener');
        $dni= $this->input->post('dni');
        $nombres= $this->input->post('nombres');
        $apellid = $this->input->post('apellid');
        $telefcEL = $this->input->post('telefcEL');
        $email = $this->input->post('email');
        $pass = $this->input->post('pass');
        $rublo = $this->input->post('rublo');
        $contr=$this->encriptar($pass);
        $persona=array('RazonSocial'=>$razon,'Direccion'=>$direc,'Telefono'=>$telef,
            'Genero'=>$gener,'DNI'=>$dni,'Nombres'=>$nombres,'Apellidos'=>$apellid,
            'TelfCel'=>$telefcEL,'TipoDePersona'=>$tipPer,'RUC'=>$ruc,'email'=>$email
            ,'Rublo'=>$rublo);
        $idPer=$this->Usuario_model->insertarPersona($persona);
        $usuario=array('IdUsuario'=>$idPer,'Password'=>$contr,'Email'=>$email
            ,'TipoUsuario'=>'E','Estado'=>'A');
        $this->Usuario_model->insertarUsuario($usuario);        
                  
            $data = array(
                'username' => $razon,
                'user_id' => $idPer,
                'role_id' => 'E',
                'logged_in' => TRUE
          );
        
         $this->session->set_userdata('logged_in',$data);

//            if ($user == "personal"){  //temporal
//                redirect('personal','refresh');
//            }else redirect('paciente','refresh');
         redirect('home/inicioSesion','refresh');         
                
        }
        
        public function validarEmail(){
            $crea = $this->input->get('creac');
            $email = $this->input->get('email');
            if (!isset($crea)){
                 $session_data = $this->session->userdata['logged_in'];
                $user=$session_data['user_id'];                
                $empresario = $this->Usuario_model->verificarEmail($email,$user);
                if(count($empresario)!=0){
                    echo "<input type=\"hidden\" id=\"invalido\" value=\"1\"><div class=\"alert alert-error\"><h4>¡Aviso!</h4>El email ya existe. Ingrese otro email.</div>";
                } else echo "<input type=\"hidden\" id=\"invalido\" value=\"0\"><div class=\"alert alert-success\"><h4>¡OK!</h4>El email esta libre.</div>";;
            }
            else {
                 $empresario = $this->Usuario_model->verificarEmail2($email);
                if(count($empresario)!=0){
                    echo "<input type=\"hidden\" id=\"invalido\" value=\"1\"><div class=\"alert alert-error\"><h4>¡Aviso!</h4>El email ya existe. Ingrese otro email.</div>";
                } else echo "<input type=\"hidden\" id=\"invalido\" value=\"0\"><div class=\"alert alert-success\"><h4>¡OK!</h4>El email esta libre.</div>";;
            }
           
        }
    
}

?>
